<?php

function getCoachResults($leagueId, $round, $year, $isArchive) {
	$roundId = getRound($round, $year, $isArchive);
	$teams = getTeams($leagueId, $isArchive);
	$teamResults = array();
	$teamCount = count($teams);
	for ($i = 0; $i < $teamCount; $i++) {
		$coachResult = new TeamResult($teams[$i]);
		$teamResults[] = $coachResult;
	}
	
	
	$fixtureRounds = getFixtureUpToRound($leagueId, $round, $isArchive);
	$settings = getSystemSettings($leagueId);
	$roundCount = count($fixtureRounds);
	for ($i = 0; $i < $roundCount; $i++) {
		$games = $fixtureRounds[$i];
		$gameCount = count($games->fixtures);
		for ($j = 0; $j < $gameCount; $j++) {
			$homeTeamScore = getPlayerListTotal($games->fixtures[$j]->homeTeamId, $games->fixtures[$j]->round, $isArchive);
			$homePenalty = getPenalty($games->fixtures[$j]->homeTeamId, $games->fixtures[$j]->round, $isArchive, $settings);
			if ($homePenalty != 0) {
				$homeTeamScore += $homePenalty;
			}
			$awayTeamScore = getPlayerListTotal($games->fixtures[$j]->awayTeamId, $games->fixtures[$j]->round, $isArchive);
			$awayPenalty = getPenalty($games->fixtures[$j]->awayTeamId, $games->fixtures[$j]->round, $isArchive, $settings);
			if ($awayPenalty != 0) {
				$awayTeamScore += $awayPenalty;
			}
			$homeTeamResult = getTeamResult($teamResults, $games->fixtures[$j]->homeTeamId);
			$awayTeamResult = getTeamResult($teamResults, $games->fixtures[$j]->awayTeamId);
			
			if ($homeTeamScore > $awayTeamScore) {
				$homeTeamResult->addWin();
				$homeTeamResult->doStreak(true);
				$awayTeamResult->doStreak(false);
			}
			else if ($homeTeamScore == $awayTeamScore) {
				$awayTeamResult->addDraw();
				$homeTeamResult->addDraw();
			}
			else {
				$awayTeamResult->addWin();
				$homeTeamResult->doStreak(false);
				$awayTeamResult->doStreak(true);
			}
			
			$homeTeamResult->addFor($homeTeamScore);
			$homeTeamResult->addAgainst($awayTeamScore);
			$awayTeamResult->addFor($awayTeamScore);
			$awayTeamResult->addAgainst($homeTeamScore);
		}
	}
	
	usort($teamResults, "compareTeamResult");
	
	return $teamResults;
}

function getGameResults($round, $leagueId, $year, $isArchive) {
	$teams = getTeams($leagueId, $isArchive);
	
	$gameResultList = array();
	$roundId = getRound($round, $year, $isArchive);
	$games = getFixture($roundId, $leagueId, $isArchive);
	$count = count($games);
	$resultArray = array();
	for ($i = 0; $i < $count; $i++) {
		$resultArray[] = getGameResult($roundId, $games[$i]->homeTeamId, $games[$i]->awayTeamId, $isArchive);
	}
	
	return $resultArray;
}

function getGameResult($roundId, $homeTeamId, $awayTeamId, $isArchive) {
	$homeScores = getPlayerScores($homeTeamId, $roundId, $isArchive);
	$penalty = getPenalty($homeTeamId, $roundId, $isArchive);
	if ($penalty != 0) {
		$playerScore = new PlayerScore();
		$player = new Player();
		$player->playerFirstName = "Penalty";
		$playerScore->player = $player;
		$playerScore->score = $penalty;
		
		$homeScores[] = $playerScore;
	}

	$awayScores = getPlayerScores($awayTeamId, $roundId, $isArchive);
	$penalty = getPenalty($awayTeamId, $roundId, $isArchive);
	if ($penalty != 0) {
		$playerScore = new PlayerScore();
		$player = new Player();
		$player->playerFirstName = "Penalty";
		$playerScore->player = $player;
		$playerScore->score = $penalty;
		
		$awayScores[] = $playerScore;
	}
	
	$gameResult = new GameResult();
	
	$gameResult->homeTeam = getTeamById($homeTeamId, $isArchive);
	$gameResult->awayTeam = getTeamById($awayTeamId, $isArchive);
	$gameResult->homeScores = $homeScores;
	$gameResult->awayScores = $awayScores;
	
	return $gameResult;
}

function getTeamResult($teamResults, $teamId) {
	$count = count($teamResults);
	for ($i = 0; $i < $count; $i++) {
		if ($teamResults[$i]->team->id == $teamId) {
			return $teamResults[$i];
		}
	}
}

function getPlayerListTotal($teamId, $round, $isArchive) {
	$playersForRound = getPlayersForRound($teamId, $round, $isArchive);
	
	$count = count($playersForRound);
	$total = 0;
	for ($i = 0; $i < $count; $i++) {
		if ($playersForRound[$i]->replacementPlayerId == null) {
			$total += getPlayerScore($playersForRound[$i]->playerId, $round, $isArchive);
		}
		else {
			$total += getPlayerScore($playersForRound[$i]->replacementPlayerId, $round, $isArchive);
		}
	}
	return $total;
}

function displayGameResults($gameResults, $separator) {
	$count = count($gameResults);
	$result = "<table width=100%>";
	for ($i = 0; $i < $count; $i++) {
		$result .= "<tr>";
		$result .= "<td width='20%'><b>" . $gameResults[$i]->homeTeam->teamName . "</b></td>";
		$result .= "<td width='15%'>" . $gameResults[$i]->getHomeCoachScore() . "<i>(" . $gameResults[$i]->getHomeNumberPlayersPlayed() . " played)</i></td>";
		$result .= "<td width='15%' align='center'>";
		if ($gameResults[$i]->isHomeTeamWin()) {
			$result .= "<i>defeated</i>";
		}
		else {
			$result .= "<i>defeated by</i>";
		}
		$result .= "</td>";
		$result .= "<td width='20%'><b>" . $gameResults[$i]->awayTeam->teamName . "</b></td>";
		$result .= "<td width='15%'>" . $gameResults[$i]->getAwayCoachScore() . "<i>(" . $gameResults[$i]->getAwayNumberPlayersPlayed() . " played)</i></td>";
		$result .= "</tr>";
		
		if (($i + 1) == $separator) {
			$result .= "<tr><td>&nbsp;</td></tr>";
		}
	}
	$result .= "</table>";
	
	return $result;
}

?>
